package app.common.utils;

import app.common.utils.cjt.ao.WmsStockoutSalesQueryWithDetailAo;
import app.common.utils.wdt.sdk.taobao.Utils;
import com.alibaba.fastjson.JSON;
import com.qimencloud.api.DefaultQimenCloudClient;
import com.qimencloud.api.QimenCloudClient;
import com.qimencloud.api.scene3ldsmu02o9.request.WdtWmsStockoutSalesQuerywithdetailRequest;
import com.qimencloud.api.scene3ldsmu02o9.response.WdtWmsStockoutSalesQuerywithdetailResponse;
import com.taobao.api.ApiException;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;

/**
 * 旺电通接口
 * @author feizns
 * @since 2023/04/04
 */
public abstract class QmUtils {

    /**
     * 自卫队
     */
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 服务器url
     */
    private static final String serverUrl = "http://3ldsmu02o9.api.taobao.com/top/router/qm";

    /**
     * 自己的appkey
     * 34331853
     * 2368249
     */
    private static final String appKey = "34331853";

    /**
     * 自己的appsecret
     * f169f72d0be4fd0ef4412dbd494fafaa
     * URTQ8oHUSx
     */
    private static final String appSecret = "f169f72d0be4fd0ef4412dbd494fafaa";

    /**
     * WDT的appkey
     */
    private static final String targetAppKey = "21363512";

    /**
     * wdt应用关键
     */
    private static final String wdtAppKey = "34331853";

    /**
     * wdt应用秘密
     */
    private static final String wdtAppSecret = "af5ed5b3dbd456357748fb995510fff5:45629849787249829634d47028365251";

    /**
     * wdt盐
     */
    private static final String wdtSalt = wdtAppSecret.split(":")[1];

    /**
     * wdt秘密
     */
    private static final String wdtSecret = wdtAppSecret.split(":")[0];

    /**
     * 客户端
     */
    private static final QimenCloudClient client = new DefaultQimenCloudClient(serverUrl, appKey, appSecret);

    public static void main(String[] args) throws ApiException {
        wdtWmsStockoutSalesQuerywithdetail(new WmsStockoutSalesQueryWithDetailAo(LocalDateTime.now().minusHours(1)), o -> System.out.println(JSON.toJSONString(o)));
    }

    /**
     * 销售querywithdetail wdt wms缺货
     * @param ao 请求参数
     * @throws ApiException api例外
     */
    public static List<WdtWmsStockoutSalesQuerywithdetailResponse.Order> wdtWmsStockoutSalesQuerywithdetail(WmsStockoutSalesQueryWithDetailAo ao) throws ApiException {
        List<WdtWmsStockoutSalesQuerywithdetailResponse.Order> orders = new ArrayList<>();
        wdtWmsStockoutSalesQuerywithdetail(ao, orders::add);
        return orders;
    }

    /**
     * 销售querywithdetail wdt wms缺货
     * @param ao 请求参数
     * @param consumer 消费者
     * @throws ApiException api例外
     */
    public static void wdtWmsStockoutSalesQuerywithdetail(WmsStockoutSalesQueryWithDetailAo ao, Consumer<WdtWmsStockoutSalesQuerywithdetailResponse.Order> consumer) throws ApiException {
        long pageNo = 1;
        long maxPageSize = 500;
        long totalCount;
        do {
            WdtWmsStockoutSalesQuerywithdetailRequestPlus request = new WdtWmsStockoutSalesQuerywithdetailRequestPlus();
            request.setParams(ao.toParams());;
            WdtWmsStockoutSalesQuerywithdetailRequest.Pager pager = new WdtWmsStockoutSalesQuerywithdetailRequest.Pager();
            pager.setPageNo(pageNo);
            pager.setPageSize(maxPageSize);
            request.setPager(pager);
            //计算签名
            request.wdtSign();
            WdtWmsStockoutSalesQuerywithdetailResponse response = client.execute(request);
            if ( !response.isSuccess() )
                throw new RuntimeException(response.getBody());
            totalCount = response.getData().getTotalCount();
            System.out.println("PageNo: " + pageNo + "  PageSize:" + maxPageSize + " Total:" + totalCount);
            response.getData().getOrder().parallelStream().forEach(consumer);
        } while ( pageNo++ * maxPageSize < totalCount );
    }

    public static class WdtWmsStockoutSalesQuerywithdetailRequestPlus extends WdtWmsStockoutSalesQuerywithdetailRequest {
        public WdtWmsStockoutSalesQuerywithdetailRequestPlus() {
            putOtherTextParam("wdt3_customer_id", "weikang3");
            setTargetAppKey(QmUtils.targetAppKey);
            setWdtAppkey(QmUtils.wdtAppKey);
            setWdtSalt(QmUtils.wdtSalt);
            setDatetime(QmUtils.sdf.format(new Date()));
        }

        public void wdtSign() {
            setWdtSign(Utils.getQimenCustomWdtSign(this, QmUtils.wdtSecret));
        }

    }

}
